package com.xinyue.game.service.framwork.context;

import com.xinyue.cross.server.server.IHandlerMessageListener;
import com.xinyue.game.message.GameServerMessageModel;
import com.xinyue.game.message.GameServerRequestHeader;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.context.ApplicationContext;

public class GameServerRequestListener implements IHandlerMessageListener {
    private ApplicationContext context;

    public GameServerRequestListener(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public void handlerMessage(GameServerMessageModel<GameServerRequestHeader> messageModel, ChannelHandlerContext ctx) {

        GameServerRequestHeader header = messageModel.getHeader();
        // 记录网关连接
        GameUserChannelCacheManager gameUserChannelCacheManager = context.getBean(GameUserChannelCacheManager.class);
        GameUserChannel gameUserChannel = gameUserChannelCacheManager.getGameUserChannel(header.getUserKey());
        if (gameUserChannel == null) {
            gameUserChannel = new GameUserChannel(ctx.channel().eventLoop(), header.getClientInstanceId());
            gameUserChannelCacheManager.addCache(header.getUserKey(), gameUserChannel);
        }
        gameUserChannel.handlerRequestMessage(messageModel, ctx);

    }
}
